package ObjectClass

/**
 * unapply方法用于对对象进行解构操作，与apply方法类似，该方法也会被自动调用
 * 可以认为unapply方法是apply方法的反向操作，apply方法接受构造参数变成对象，而unapply方法接受一个对象，从中提取值
 */
object upApply {
  def main (args: Array[String]) {
    val Car02(carbrand,carprice) = Car02("BMW",800000)
    println("brand: "+carbrand+" and carprice: "+carprice)
  }

}
class Car02(val brand:String,val price:Int) {
  def info() {
    println("Car brand is "+ brand+" and price is "+price)
  }
}
object Car02{
  def apply(brand:String,price:Int)= {
    println("Debug:calling apply ... ")
    new Car02(brand,price)
  }
  def unapply(c:Car02):Option[(String,Int)]={
    println("Debug:calling unapply ... ")
    Some((c.brand,c.price))
  }
}
